
package me.tomato3017.plugins.epicmusic.audio;

import me.tomato3017.plugins.epicmusic.EpicMusic;
import org.bukkit.entity.Player;
import org.getspout.spoutapi.SpoutManager;
import org.getspout.spoutapi.player.SpoutPlayer;
import org.getspout.spoutapi.sound.SoundManager;

/**
 *
 * @author Anthony Kirksey <Anthony.Kirksey@gmail.com>
 * Purpose: Responsible for interactions to the SPOUT API audio calls.
 */
public class AudioManager {
    EpicMusic plugin;

    public AudioManager(EpicMusic plugin) {
        this.plugin = plugin;
    }
    
    /*
     * playMusic((MusicData musicData, String player))
     * Purpose: Sends the requested music to the SpoutAPI to be played.
     */
    public void playMusic(MusicData musicData, Player player){
        SoundManager sManager = SpoutManager.getSoundManager();
        
        if((musicData == null) || (musicData.getURL() == null)){
            return;
        }
        
        if(player instanceof Player){
            if(EpicMusic.isPlayerDisabled(player))
                return;
            
            SpoutPlayer sp = (SpoutPlayer) player;
            
            if(sp.isSpoutCraftEnabled() && sp.isOnline()){
                if(EpicMusic.isDEBUG()){
                    EpicMusic.debugLog("Playing music alias:" + musicData.getAlias() + " for player " + player.getName());
                }
                sManager.playCustomMusic(plugin, sp, musicData.getURL(), false, sp.getLocation(), -1, musicData.getVolume());
            }
        }
    }
    
    /*
     * playAmbient(AmbientMusicData ambientMusicData, String player)
     * Purpose: Plays the sound effect at the requested players location.
     */
    public void playAmbient(AmbientMusicData ambientMusicData, Player player, int distance){
        SoundManager sManager = SpoutManager.getSoundManager();
        
        if((ambientMusicData == null) || (ambientMusicData.getURL() == null)){
            return;
        }
        
        if(player instanceof Player){
            if(EpicMusic.isPlayerDisabled(player))
                return;
            
            SpoutPlayer sp = (SpoutPlayer) player;
            
            if(sp.isSpoutCraftEnabled() && sp.isOnline()){
                if(EpicMusic.isDEBUG()){
                    EpicMusic.debugLog("Sound effect " + ambientMusicData.getAlias() + " for player " + player.getName(), 2);
                }
                
                sManager.playCustomSoundEffect(plugin, sp, ambientMusicData.getURL(), false, player.getLocation(), distance, ambientMusicData.getVolume());
            }
        }

    }
    
    public void stopMusic(Player player){
        if(player instanceof Player){
            SoundManager sManager = SpoutManager.getSoundManager();
            sManager.stopMusic((SpoutPlayer) player, false, 4000);
        }
    }
}
